package cn.rslee.server;

import java.util.List;

/**
 * 描述：
 * 
 * <pre>HISTORY
 * ****************************************************************************
 *  ID   DATE           PERSON          REASON
 *  1    2016年6月16日         rslee            Create
 * ****************************************************************************
 * </pre>
 * @author rslee
 * @since 1.0
 */
public  class SimplePage<T> implements IPage<T> {
	
	private List<T> datas;
	
	private long total;
	
	private int pageSize;
	
	private int currentPage;
	
	private long from;
	
	private long to;
	
	private long totalPage;
	
	public SimplePage(List<T> datas,long total,int currentPage,int pageSize){
		this.datas = datas;
		this.total = total;
		this.currentPage = currentPage;
		this.pageSize = pageSize;
		
		this.from = calculateFrom(currentPage,pageSize);
		
		calculateTotalPage();
		calculateTo();
		
	}
	
	public static int calculateFrom(int currentPage,int pageSize){
		return currentPage<1 ?0:(currentPage-1)*pageSize;
	}
	
	private void calculateTo(){
		long to = currentPage*pageSize;
		this.to = to > total ? total : to;
	}
	
	/**
	 * 计算总页数
	 * void
	 */
	private void calculateTotalPage(){
		if(total % pageSize == 0){
			totalPage = total / pageSize;
		}else{
			totalPage = total / pageSize + 1;
		}
	}

	@Override
	public List<T> getDatas() {
		return this.datas;
	}

	@Override
	public long getTotal() {
		return this.total;
	}

	@Override
	public long getPageSize() {
		return this.pageSize;
	}

	@Override
	public long getCurrentPage() {
		return this.currentPage;
	}

	@Override
	public long getFrom() {
		return this.from;
	}

	@Override
	public long getTo() {
		return this.to;
	}

	@Override
	public long getTotalPage() {
		return this.totalPage;
	}

}
